package com.example.arithmeticleetcode.leetcode.dec;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-31 15:38
 **/
public class Demo30 {

    public static void main(String[] args) {
        int[][] arr = {{1, 2}, {2, 3}, {3, 4}, {1, 3}};
        int[][] arr2 = {{1, 100}, {11, 22}, {1, 11}, {2, 12}};
        eraseOverlapIntervals(arr);
        eraseOverlapIntervals(arr2);
    }

    public static int eraseOverlapIntervals(int[][] intervals) {
        if (intervals == null || intervals.length == 0) return 0;
        Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
        int count = 0;
        for (int i = 1; i < intervals.length; i++) {
            int preIndex = i - 1;
            if (intervals[preIndex][1] > intervals[i][0]) {
                count++;
                if (intervals[preIndex][1] > intervals[i][1]) continue;
                int[] temp = intervals[preIndex];
                intervals[preIndex] = intervals[i];
                intervals[i] = temp;
            }
        }
        return count;
    }
}
